using log4net;
using System;
using System.IO;

namespace Pay.Core {
    public class LogHelper : ILogHelper {
        private readonly ILog infoLogger = null;
        public LogHelper(string name) {
            string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            if (!Directory.Exists(Path.Combine(path, "log", name)))
                Directory.CreateDirectory(Path.Combine(path, "log", name));
            string logFile = Path.Combine(path, "log", name + ".config");
            if (!File.Exists(logFile)) {
                var logsetString = File.ReadAllText(Path.Combine(path, "log4net.config")).Replace("{name}", name);
                File.WriteAllText(logFile, logsetString);
            }
            log4net.Config.XmlConfigurator.Configure(new FileInfo(logFile));
            infoLogger = LogManager.GetLogger(name);
        }

        public void Error(string info) {
            if (infoLogger.IsErrorEnabled)
                infoLogger.Error(info);
        }
        public void Error(string info, Exception ex) {
            if (infoLogger.IsErrorEnabled)
                infoLogger.Error(info, ex);
        }
        public void Info(string info) {
            if (infoLogger.IsInfoEnabled)
                infoLogger.Info(info);
        }
    }
}
